<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="哈希长度扩展攻击"/>




  <meta name="keywords" content="hash, 拓展攻击, 八一" />



  <meta name="baidu-site-verification" content="HhUstaSjr0" />



  <meta name="google-site-verification" content="UA-102975942-1" />






  <link rel="alternate" href="/atom.xml" title="八一">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="https://bay1.top/2017/05/12/哈希长度扩展攻击/"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />
<link rel="stylesheet" type="text/css" href="/css/prettify.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/sons-of-obsidian.css" media="screen" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  
  <script id="baidu_analytics">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?9a885cc9fb6cd7bcef579deb8efe8a70";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>



  <script id="google_analytics">
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-102975942-1', 'auto');
        ga('send', 'pageview');
  </script>










    <title> 哈希长度扩展攻击 - 八一 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">八一</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/archives">
        <li class="mobile-menu-item">
          
          
            文章
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            标签
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            关于/友链
          
        </li>
      </a>
    
      <a href="/search">
        <li class="mobile-menu-item">
          
          
            站内搜索
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">八一</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives">
            
            
              文章
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              标签
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              关于/友链
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/search">
            
            
              站内搜索
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          哈希长度扩展攻击
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2017-05-12
        </span>
        
        
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#md5长度拓展攻击"><span class="toc-text">md5长度拓展攻击</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#md5算法"><span class="toc-text">md5算法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#对md5实现过程"><span class="toc-text">对md5实现过程</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#sha1长度拓展攻击"><span class="toc-text">sha1长度拓展攻击</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#sha1算法"><span class="toc-text">sha1算法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结尾"><span class="toc-text">结尾</span></a></li></ol>
    </div>
  </div>


    <div class="post-content">
      
        <p>说实话，哈希长度拓展攻击的原理真的很难理解，至少我的智商，懂起来很吃力 <a id="more"></a></p>
<h2 id="md5长度拓展攻击"><a href="#md5长度拓展攻击" class="headerlink" title="md5长度拓展攻击"></a>md5长度拓展攻击</h2><blockquote>
<p>这也是学校入门赛的一道题目，然后才去了解了一下哈希长度拓展攻击，因为拓展攻击适用情况，所以最常见md5，sha1加密<br>学校的这道题目是关于md5的，而能够实现这种攻击是需要前提条件的<br>hash=md5(密文)–&gt;forge_hash=(密文+可利用信息)<br><span style="color: red;">知道密文MD5和长度</span></p>
</blockquote>
<h3 id="md5算法"><a href="#md5算法" class="headerlink" title="md5算法"></a>md5算法</h3><blockquote>
<p>对一个字符串md5，首先要进行补位，即他要满足len(字符串二进制) % 512 == 448，或者说是他二进制长度为512的倍数减64<br>然后要进行补长度，就是他自身长度字节转到bit再转到16进制+若干000直到补长度达到64位<br>这个时候就能512位进行分组，计算过程是512位进行一次运算，也就是如果消息长度小于512就只需要计算一次，大于512小于1024是计算两次,以此类推。<br>计算消息摘要必须用补位已经补长度完成之后的消息来进行运算，具体过程，很复杂。。。。下面是重点！<br>经过一次消息摘要后，上面的链变量将会被新的值覆盖，而最后一轮产生的链变量经过高低位互换（如：123456-&gt; 654321）后就是我们计算出来的md5值</p>
</blockquote>
<h3 id="对md5实现过程"><a href="#对md5实现过程" class="headerlink" title="对md5实现过程"></a>对md5实现过程</h3><blockquote>
<p>下面是这道题目的主要代码</p>
</blockquote>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(stripos($role,<span class="string">"admin"</span>)!==<span class="keyword">false</span>&amp;&amp;$hsh===md5($salt.$_COOKIE[<span class="string">"role"</span>])</span><br><span class="line">$hsh=$_COOKIE[<span class="string">"hsh"</span>];$role=$_COOKIE[<span class="string">"role"</span>];</span><br><span class="line">&#123;<span class="keyword">if</span>(<span class="keyword">isset</span>($_COOKIE[<span class="string">"role"</span>]))</span><br><span class="line">$salt=<span class="string">"xxxxxxxxxxxxx"</span>;<span class="comment">//len:13</span></span><br><span class="line">$role=<span class="string">"guest"</span>;</span><br><span class="line">$auth=<span class="keyword">false</span>;</span><br></pre></td></tr></table></figure>
<p><img src="https://s1.ax1x.com/2018/01/01/pSR4EV.png" alt="hash"></p>
<blockquote>
<p>看到代码我们可以知道三点，role，secret长度(密文长度)，hash<br>接下来我们进行哈希长度扩展攻击<br>首先我们可以按照md5算法来手工完成，就是补位-&gt;补长度-&gt;计算他的链变量（网上有计算代码）-&gt;高低位互换</p>
</blockquote>
<p>PS:不大于512，计算一次，大于512位，先补全为1024位，类推</p>
<blockquote>
<p>另一种方法是使用工具，，，我还是喜欢这一种！。。。。Hashpump</p>
</blockquote>
<p><strong>linux下的安装：</strong><br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">git clone https://github.com/bwall/HashPump</span><br><span class="line">apt-get install g++ libssl-dev</span><br><span class="line">cd HashPump</span><br><span class="line">make</span><br><span class="line">make install</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>然后在linux执行<span style="color: red;">hashpump -s c70ab9d039f166c5b5f506a5698ebaa1 -d guest -k 13 -a admin</span></p>
</blockquote>
<p><img src="https://s1.ax1x.com/2018/01/01/pSRTCF.md.png" alt="Hashpump"></p>
<blockquote>
<p>然后把hash放到hsh,role取下面那一串串，你就绕过变成了admin。</p>
</blockquote>
<h2 id="sha1长度拓展攻击"><a href="#sha1长度拓展攻击" class="headerlink" title="sha1长度拓展攻击"></a>sha1长度拓展攻击</h2><h3 id="sha1算法"><a href="#sha1算法" class="headerlink" title="sha1算法"></a>sha1算法</h3><p>引用freebuf的一段话：</p>
<blockquote>
<p>首先，当hash函数拿到需要被hash的字符串后，先将其字节长度整除64，取得余数。如果该余数正好等于56，那么就在该字符串最后添加上8个字节的长度描述符（具体用bit表示）</p>
</blockquote>
<p>如果不等于56，就先对字符串进行长度填充，填充时第一个字节为hex(80)，其他字节均用hex(00)填充</p>
<blockquote>
<p>填充至余数为56后，同样增加8个字节的长度描述符（该长度描述符为需要被hash的字符串的长度，不是填充之后整个字符串的长度）。以上过程，称之为补位。</p>
</blockquote>
<p>说白了其实这个sha1和md5是类似的，只是稍有不同！(len(字符串二进制) % 64 == 56)</p>
<h2 id="结尾"><a href="#结尾" class="headerlink" title="结尾"></a>结尾</h2><blockquote>
<p>这个过程理解起来，感觉的确是一时间可能不能完全理解，不过明白之后可能感觉也很简单了<br>不过，至于我，还是没完全理解，似懂非懂吧。</p>
</blockquote>

      
    </div>

    
      
      



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/hash/">hash</a>
            
              <a href="/tags/拓展攻击/">拓展攻击</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/2017/05/17/痛苦的数据结构OJ/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">痛苦的数据结构OJ</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    
    
      <a class="next" href="/2017/05/09/SSCTF2017部分WP/">
        <span class="next-text nav-default">SSCTF2017部分WP</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div> 
    </div>
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="https://github.com/bay1" class="iconfont icon-github" title="github"></a>
        
      
    
      
        
          <a href="http://weibo.com/3190704711/profile?topnav=1&wvr=6&is_all=1" class="iconfont icon-weibo" title="weibo"></a>
        
      
    
      
    
      
    
      
    
    
    
  </div>


<div class="copyright">
  <span class="copyright-year">
    
    &copy; 
     
      2016 - 
    
    2018
    <span class="author">bay1</span>
  </span>
</div>
      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://bay1.top/2017/05/12/哈希长度扩展攻击/';
        this.page.identifier = '2017/05/12/哈希长度扩展攻击/';
        this.page.title = '哈希长度扩展攻击';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//https-blog-flywinky-top-1.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>



    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>
<script src="/js/prettify.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $('pre').addClass('prettyprint');
   prettyPrint();
 })
</script>
  </body>
</html>
